﻿$(document).keydown(
		function(event) {
			if (event.keyCode == 112) {
				window.location = '0.menu.osgc';
			}			
		});
$.ajaxSetup ({ 
	cache: false
});

$(document).ready(function(){
		$("a#a_saveecode").unbind().click(function(){
		    $("form#saveecode").submit();
		});	
		
		
		$("form#Login").submit(function(){
			if($("input#userid").val().length==0){
				alert("请录入登录ID!");
				$("input#inqty").focus();
				return false;
			}
		});
		$("form#PosQuery").submit(function(){
			if($("input#posno").val().length==0){
				alert("请扫描货位。");
				$("input#posno").val("");
				$("input#posno").focus();
				return false;
			}
		});

		$("form#BoundTurnBox").submit(function(){
			if($("input#containerno").val().length==0){
				alert("请扫描周转箱。");
				$("input#containerno").val("");
				$("input#containerno").focus();
				return false;
			}
		});
	
			var oldlothtml =  $("#createlotno").html();
		$("input#gennewlot").toggle(function(){
				var approvedocno = $('#goods_approvedocno').val();
				var registno = $('#goods_registno').val();
				var prodlicenseno = $('#goods_prodlicenseno').val();
				var s_periodunit = $('#s_periodunit').val();
				var l_period = $('#l_period').val();
				var date = new Date();
				var year =  date.getFullYear();
				var month = date.getMonth()+1;
				var day = date.getDate();//<input onClick="checkKeyBoard(this,event)" type="text" name="lotno" id="newlotno"/>
				var lothtml='<dt class="new_lot_dt">批&nbsp;&nbsp;&nbsp;&nbsp;号：<input type="text" name="lotno" id="newlotno"/></dt>'
				+'<dt class="new_lot_dt">灭菌批号：<input type="text" name="killlotno"/></dt>'
				+'<dt class="new_lot_dt">有效期至：<input type="text" name="validdate_year" width="50px" value="'+(year+2)+'" size=4 />年'
				+'<input type="text" name="validdate_month" width="25px" value="'+month+'" size=2 />月'
				+'<input type="text" name="validdate_date" width="25px" value="'+day+'" size=2 />日</dt>'
				+'<dt class="new_lot_dt">生产日期：<input type="text" name="proddate_year" id="proddate_year_a" width="50px" value="'+year+'" size=4 />年'
				+'<input type="text" name="proddate_month" id="proddate_month_b" width="25px"  value="'+month+'" size=2 />月'
				+'<input type="text" name="proddate_date" id="proddate_date_c" width="25px" value="'+day+'" size=2 />日</dt>'
				+'<dt class="new_lot_dt">批准文号：<input type="text" name="approvedocno" value="'+approvedocno+'"/></dt>'
				+'<dt class="new_lot_dt">注册证号：<input type="text" name="registno"  value="'+registno+'"/></dt>'
				+'<dt class="new_lot_dt">生产许可证号：<input type="text" name="prodlicenseno"  value="'+prodlicenseno+'"/></dt>';
				 $("#createlotno").after(lothtml);
			 	$("#createlotno").empty();	
			 	$("#createlotno").hide();
				$("input#newlotno").focus();
				$("input#newlotno").blur(function(){
					var lno = $("input#newlotno").val();
					if(lno.length>0){
						var strP=/^\d+(\.\d+)?$/; 
						if(!strP.test(lno)) 
							return false; 
						if(lno.length<=7){
							lno = "20"+lno;
						}
						if(lno.length>8){
							lno = lno.substring(0,8);
						}
						var d  = lno.substring(0,4)+"/"+lno.substring(4,6)+"/"+lno.substring(6,8);
						var date = new   Date (Date.parse(d));
						var daytmp = date.getDate();
						var year = date.getYear();
						var yeartmp = (year<1900?(1900+year):year); 
						var monthtmp = date.getMonth()+1;
						$("input[name='proddate_year']").val(yeartmp);
						$("input[name='proddate_month']").val(monthtmp);
						$("input[name='proddate_date']").val(daytmp);
						if(s_periodunit=="3" || s_periodunit=="日"){
							var b = parseInt(l_period);
							if(b%30==0){
								var c = parseInt((b+daytmp)/30);
								monthtmp = parseInt(monthtmp) + c;
							}else{
								var a = (b+daytmp)%30;
								var c = parseInt((b+daytmp)/30);
								daytmp = parseInt(daytmp) + parseInt(a);
								monthtmp = c;
								
								
							}
							
						}else if(s_periodunit=="2" || s_periodunit=="月"){
							var d = parseInt(l_period);
							if(d%12==0){
								var f = parseInt((d+monthtmp)/12);
								yeartmp = parseInt(yeartmp) + parseInt(f);
							}else{
								var e = (d+monthtmp)%12;
								var f = parseInt((d+monthtmp)/12);
								yeartmp = parseInt(yeartmp) + parseInt(f);
								monthtmp = e;
							}
							
						}else if(s_periodunit=="1" || s_periodunit=="年"){
							yeartmp = parseInt(yeartmp) + parseInt(l_period);
						}
						
						$("input[name='validdate_year']").val(yeartmp);
						$("input[name='validdate_month']").val(monthtmp);
						$("input[name='validdate_date']").val(daytmp);
					}
				});
				
				$(this).val("返回选择批号");
		},function(){
			$(this).val("新增批号");
				$(".new_lot_dt").remove();
				$("#createlotno").html(oldlothtml);	
				//closekeyboard();
				$("#createlotno").show();
			});
		
		var oldpackhtml =  $("#createpack").html();
		$("input#gennewpack").toggle(function(){				
				var packhtml = '<dt class="new_pack_dt">包装名称：<input type="text" name="goodspackname" id="newpackname"/></dt>'
				+'<dt class="new_pack_dt">包装大小：<input type="text" name="packsize"/></dt>'
				+'<dt class="new_pack_dt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;长：<input type="text" name="goodslength"/> CM</dt>'
				+'<dt class="new_pack_dt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;宽：<input type="text" name="goodswidth"/> CM</dt>'
				+'<dt class="new_pack_dt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高：<input type="text" name="goodsheight"/> CM</dt>'
				+'<dt class="new_pack_dt">体&nbsp;&nbsp;&nbsp;&nbsp;积：<input type="text" name="goodscubage"/> M<sup>3</sup></dt>'
				+'<dt class="new_pack_dt">重&nbsp;&nbsp;&nbsp;&nbsp;量：<input type="text" name="goodsweight"/> KG</dt>';
			 
			 $("#createpack").after(packhtml);
			 $("#createpack").empty();
			 $("#createpack").hide();
			 $("input#newpackname").focus().change(function(){
			 $('#qty_goodspackname').html($(this).val());
			 });
			 $(this).val("返回选择包装");
		},function(){
				$(this).val("新增包装");
				$(".new_pack_dt").remove();
				$("#createpack").html(oldpackhtml);	
				$("#createpack").show();
			});
		
			

		$("input#boxbarcode").keydown(function(event){
					if(event.keyCode==13){
						$("input#outboxbarcode").focus();}
				});
				
		$("input#outboxbarcode").keydown(function(event){
					if(event.keyCode==13){
						$("input#button").focus();}
				});
		$("form#bandoutbox").submit(function(){
			if($("input#boxbarcode").val().length==0){
				alert("请扫描装箱条码。");
				$("input#boxbarcode").val("");
				$("input#boxbarcode").focus();
				return false;
			}
			if($("input#outboxbarcode").val().length==0){
				
				$("input#outboxbarcode").val("");
				$("input#outboxbarcode").focus();
				return false;
			}
		});		
		
		$("input#movegoodsqty").keydown(function(event){
			if(event.keyCode==13){
				$("input#movetoposno").focus();}
		});	
		$("input#barcode").keydown(function(event){
			if(event.keyCode==13){
				if($("input#barcode").val().length!=0){
					$("form#changeposno").submit();
				}else{
					$("input#findgoods").focus();
				}
			
				}
		});
		
		$("form#tradegoods").submit(function(){
			if($("input#tradegoodsqty").val().length==0){
				alert("请输入调整数量。");
				$("input#tradegoodsqty").val("");
				$("input#tradegoodsqty").focus();
				return false;
			}
			return confirm("数量:"+$("input#tradegoodsqty").val()+"\n"+"批号："+$("#lotid option:selected").text()+"\n"+"货品状态:"+$("#goodsstatusid option:selected").text()+"\n"+"确定要调整吗？");
			
		});		
	
		$("input#findgoods").keydown(function(event){
			if(event.keyCode==13){
				var goodscode=$("input#findgoods").val();
				
				 $("input[name='goodsbarcode']").each( 
						
						 function(){
							
							 if(goodscode==$(this).val()){
								 $( $($(this)).parent()).parent().css("background","#bbffaa");  
							 }
						 }
				 ); 
			}
		});			
});
function setdate(datepick,yearflag){
		var id = datepick.attr('id');	
				var date = new Date();
				var thisyear = date.getYear();
        // 当前年和月
        if(navigator.userAgent.indexOf("IE") == -1)
        {
                 thisyear = thisyear + 1900;
        }
			
				var thismonth =  date.getMonth()+1;
				var thisday =  date.getDate();
				var yearselect = '<select name="'+id+'year">\n';
				var begin = thisyear;
				var end = thisyear+1;
				if(yearflag==1){
					begin = thisyear-7;
				}else{
				end = thisyear+8;
				}
				for(;begin<end;begin++){
				if(begin==thisyear){
					yearselect+='<option selected="selected">';
				}else{
					yearselect+='<option>';
				}
					yearselect+=begin;
					yearselect+='</option>\n';
				}				
				yearselect += '</select>\n';
				var monthselect = '<select name="'+id+'month">\n';
				
				for(var i = 1;i<13;i++){
				if(i==thismonth){
					monthselect+='<option selected="selected">';
				}else{
					monthselect+='<option>';
				}
					monthselect+=i;
					monthselect+='</option>\n';
				}				
				monthselect += '</select>\n';
				var dateselect = '<select name="'+id+'date">\n';
				
				for(var i = 1;i<32;i++){
				if(i==thisday){
					dateselect+='<option selected="selected">';
				}else{
					dateselect+='<option>';
				}
					dateselect+=i;
					dateselect+='</option>\n';
				}				
				dateselect += '</select>\n';
				datepick.html(yearselect+monthselect+dateselect);	
	}

/**
* 时间对象的格式化;
*/
Date.prototype.format = function(format){
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
var o = {
"M+" :  this.getMonth()+1,  //month
"d+" :  this.getDate(),     //day
"h+" :  this.getHours(),    //hour
   "m+" :  this.getMinutes(),  //minute
   "s+" :  this.getSeconds(), //second
   "q+" :  Math.floor((this.getMonth()+3)/3),  //quarter
   "S"  :  this.getMilliseconds() //millisecond
};

if(/(y+)/.test(format)) {
 format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}

for(var k in o) {
 if(new RegExp("("+ k +")").test(format)) {
   format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
 }
}
return format;
};

